package cn.org.xwb.bookmall.admin.controller;

import cn.org.xwb.bookmall.admin.annotation.IsLoginParam;
import cn.org.xwb.bookmall.model.constant.AccountConst;
import cn.org.xwb.bookmall.model.mapper.LiuImgMapper;
import cn.org.xwb.bookmall.model.mapper.LiuMapper;
import cn.org.xwb.bookmall.model.service.impl.LiuServiceImpl;
import cn.org.xwb.bookmall.model.vo.LiuVo;
import cn.org.xwb.common.encrypt.annotation.des.DesEn;
import cn.org.xwb.common.encrypt.annotation.rsa.RsaDe;
import cn.org.xwb.common.encrypt.annotation.rsa.RsaDeParam;
import cn.org.xwb.common.encrypt.constant.EncryptConstant;
import cn.org.xwb.common.error.annotation.ServerErrorCatch;
import cn.org.xwb.common.model.vo.RespResult;
import cn.org.xwb.common.page.dto.PageResult;
import cn.org.xwb.common.valid.annotation.nu.NotNull;
import cn.org.xwb.common.valid.annotation.nu.NotNullParam;
import cn.org.xwb.common.valid.annotation.str.StrNotNull;
import cn.org.xwb.common.valid.annotation.str.StrNotNullParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/liu")
public class AdminLiuController {

    @Autowired
    private LiuMapper liuMapper;

    @Autowired
    private LiuImgMapper liuImgMapper;

    @Autowired
    private LiuServiceImpl liuService;

    @GetMapping("/page")
    @ServerErrorCatch
    @StrNotNull
    @NotNull
    @RsaDe
    @DesEn
    public RespResult<Object> getLiuPage(
            @RequestParam(EncryptConstant.DES_KEY_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String desKey,
            @RequestParam("current") @NotNullParam
            Integer current,
            @RequestParam("pageSize") @NotNullParam
            Integer pageSize,
            @RequestParam(value = "userId",required = false)
            Long userId,
            @RequestParam(value = "kw", required = false) @RsaDeParam
            String kw
    ) {
        PageResult<List<LiuVo>> liuPage = liuService.getLiuPage(current, pageSize, userId, kw);
        return RespResult.success("获取留言列表成功", liuPage);
    }

    @PostMapping("/del")
    @ServerErrorCatch
    @StrNotNull
    @NotNull
    @RsaDe
    @IsLoginParam
    public RespResult<Object> delLiu(
            @RequestParam(AccountConst.ACCOUNT_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String account,
            @NotNullParam
            Long id
    ) {
        Boolean res = liuService.delLiu(id);
        if (!res) {
            return RespResult.fail("删除留言失败");
        }
        return RespResult.success("删除留言成功");
    }

}
